package com.dxy.app.widget.loading_view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Region;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import com.dxy.app.R;
import com.dxy.app.utils.ImageUtils;
import com.dxy.app.widget.ThreadManager;

/**
  *Created by duxingyu on 2017/7/25.9:34
  *Email: 
  *Description:图片类型的进度控件
  **/

public class ImageLoadingView extends View implements View.OnClickListener{

  public static final String TAG="TAG_ImageLoadingView";
  /**
   * 竖直方向上的偏移量,默认为0
   * */
  private float offsetY =0;
  /**
   * 画笔
   * */
  private Paint paint;
  /**
   * 默认的背景图片，表示进度的背景图片
   * */
  private Bitmap def,pro;
  /**
   * 控件宽高
   * */
  private float width,height;

  public ImageLoadingView(Context context) {
    super(context);
    init(null);
  }

  public ImageLoadingView(Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
    TypedArray array =context.obtainStyledAttributes(attrs,R.styleable.ImageLoadingView);
    init(array);
  }

  public ImageLoadingView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    TypedArray array =context.obtainStyledAttributes(attrs,R.styleable.ImageLoadingView);
    init(array);
  }
  /**
   * 初始化一些参数
   * */
  private void init(TypedArray array){
    if(array!=null){
      width =array.getDimensionPixelSize(R.styleable.ImageLoadingView_loading_image_width,0);
      height =array.getDimensionPixelSize(R.styleable.ImageLoadingView_loading_image_height,0);
      int defResId =array.getResourceId(R.styleable.ImageLoadingView_loading_default_image,R.mipmap.ic_favorite_empty);
      int proResId =array.getResourceId(R.styleable.ImageLoadingView_loading_progress_image,R.mipmap.ic_favorite_full);
      def =getBitmap(defResId);
      pro =getBitmap(proResId);
      array.recycle();
    }else{
      float density =getContext().getResources().getDisplayMetrics().density;
      width =100*density;
      height =100*density;
      def = getBitmap(R.mipmap.ic_favorite_empty);
      pro = getBitmap(R.mipmap.ic_favorite_full);
    }
    paint =new Paint();
    paint.setAntiAlias(true);

    setOnClickListener(this);
  }

  @Override protected void onDraw(Canvas canvas) {
    Rect rect =new Rect(0,0,(int)width,(int) height);
    canvas.drawBitmap(def,null,rect,paint);//将图片绘制到指定的区域，如果图片尺寸与指定区域不一致，那么就自动拉伸、缩放图片
    canvas.clipRect(0,offsetY,width,height, Region.Op.INTERSECT);
    canvas.drawBitmap(pro,null,rect,paint);
  }

  @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    setMeasuredDimension((int)width, (int) height);
  }

  private Bitmap getBitmap(int resId){
    return ImageUtils.decodeBitmapFromResources(getContext().getResources(),resId,(int)width, (int) height);
  }

  public void setProgress(float percent){
    offsetY=height*percent;
    invalidate();
  }


  private Handler handler =new Handler(){
    private float percent =0;
    @Override public void handleMessage(Message msg) {
      percent+=0.02f;
      setProgress(percent);
      if(percent>=1.0f){
        inGrowthing =false;
      }
    }
  };
  private boolean inGrowthing =false;
  @Override public void onClick(View v) {
    if(inGrowthing){
      return;
    }
    inGrowthing =true;
    ThreadManager.getShortPool().execute(()->{
      while (inGrowthing){
        try {
          Thread.sleep(100);
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
        handler.sendEmptyMessage(0);
      }
    });
  }
}
